import { createApp } from 'vue'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import App from './App.vue'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import { router } from './router/index.js'
import axios from 'axios'
import { createPinia } from 'pinia'
import { setupGuards } from './router/guards';

import VMdEditor from '@kangc/v-md-editor';
import '@kangc/v-md-editor/lib/style/base-editor.css';
import Prism from 'prismjs'; // 语法高亮
import '@kangc/v-md-editor/lib/theme/style/github.css'; // 主题样式
import githubTheme from '@kangc/v-md-editor/lib/theme/github.js';

//完全版

const app = createApp(App)
const pinia = createPinia()
//添加axios请求的拦截器
axios.interceptors.request.use(config => {
    //从本地读取token
    const token = localStorage.getItem('token')
    //判断token是否存在
    if (token) {
        //将token添加到请求头中
        config.headers.Authorization = `Bearer ${token}`
    }
    return config;
});

app.use(pinia)
//定义图片地址全局变量，所有组件都可以使用
app.config.globalProperties.$urlImage = 'http://localhost:8080/images/';
app.config.productionTip = false
// 指定 axios 的全局配置，指定为后端服务器地址
axios.defaults.baseURL = 'http://localhost:8080/'
app.config.globalProperties.$axios = axios
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component)
}

//设置自路由守卫
setupGuards(router);
//使用plus
app.use(ElementPlus)
app.use(router)
VMdEditor.use(githubTheme, { Prism });
app.use(VMdEditor);
app.mount('#app')